package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ValidLogon;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.omp.industry.OmpIndustry;
import com.vm.osm.business.dto.omp.logic_devices.LogicDeviceNode;
import com.vm.osm.business.dto.omp.logic_prop_relas.LogicProp;
import com.vm.osm.business.dto.omp.type_dicts.TypeDict;
import com.vm.osm.business.dto.req.logic_node.AddLogicNodeReq;
import com.vm.osm.business.dto.req.logic_node.UpdateLogicNodeReq;
import com.vm.osm.business.dto.rsp.logic_node.LogicNodeTree;
import com.vm.osm.business.service.IOsmLogicNodeService;
import com.vm.osm.business.service.omp.IOmpIndustryService;
import com.vm.osm.business.service.omp.IOmpLogicDevicesService;
import com.vm.osm.business.service.omp.IOmpLogicPropRelasService;
import com.vm.osm.business.service.omp.IOmpTypeDictsService;
import com.vm.osm.business.util.UserDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 逻辑节点信息
 *
 * @author Xuerong Xue
 */
@RestController
@RequestMapping("/v1.0/logic_node")
@Api(value = "OsmLogicNodeController", description = "逻辑节点信息", tags = {"OsmLogicNodeController"})
public class OsmLogicNodeController extends ControllerExceptionHandler {
    private Logger LOG = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IOsmLogicNodeService osmLogicNodeService;
    @Autowired
    private IOmpTypeDictsService ompTypeDictsService;
    @Autowired
    private IOmpIndustryService ompIndustryService;
    @Autowired
    private IOmpLogicDevicesService ompLogicDevicesService;
    @Autowired
    private IOmpLogicPropRelasService ompLogicPropRelasService;

    /**
     * 获取逻辑节点行业列表
     */
    @ValidLogon
    @ApiOperation(value = "获取行业列表", response = OmpIndustry[].class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/industry/list")
    public Object getIndustryList() {
        LOG.info("【逻辑节点】（获取行业列表）");
        return ompIndustryService.list();
    }

    /**
     * 根据行业Id获取节点类型列表
     */
    @ValidLogon
    @ApiOperation(value = "根据行业Id获取节点类型列表", response = LogicDeviceNode[].class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/type")
    public Object getTypeByIndustryId(@ApiParam(
            name = "industry_id", value = "行业Id", required = true) @RequestParam("industry_id") String industry_id) {
        LOG.info("【逻辑节点】（根据行业Id获取节点类型列表）-> industry_id = {}", industry_id);
        return ompLogicDevicesService.getByIndustryId(industry_id);
    }

    /**
     * 根据节点类型Id获取属性列表
     */
    @GetMapping("/type/{type_id}/props")
    @ValidLogon
    @ApiOperation(value = "根据节点类型Id获取属性列表", response = LogicProp[].class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    public Object getPropsByNodeTypeId(@ApiParam(
            name = "type_id", value = "节点类型Id", required = true) @PathVariable String type_id) {
        LOG.info("【逻辑节点】（根据节点类型Id获取属性列表）-> type_id = {}", type_id);
        return ompLogicPropRelasService.getByLogicDeviceId(type_id);
    }

    /**
     * 新增
     */
    @PostMapping
    @ValidLogon
    @ApiOperation(value = "新增")
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    public void add(HttpServletRequest servlet, @Valid @RequestBody AddLogicNodeReq request, BindingResult valid) {
        request.setEnterprise_id(UserDataUtil.getUserSelectedEnterpriseId(servlet));
        request.setCreator_id(UserDataUtil.getStaffId(servlet));
        LOG.info("【逻辑节点】（新增）-> 请求参数：{}", request);
        if (valid.hasErrors()) {
            throw new BusinessException(valid.getFieldError().getDefaultMessage());
        }
        osmLogicNodeService.add(request);
    }

    /**
     * 更新
     */
    @PutMapping("/{id}")
    @ValidLogon
    @ApiOperation(value = "更新")
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    public void update(@ApiParam(required = true, name = "id", value = "节点Id") @PathVariable String id,
                       @Valid @RequestBody UpdateLogicNodeReq request, BindingResult valid) {
        request.setId(id);
        LOG.info("【逻辑节点】（更新）-> 请求参数：{}", request);
        if (valid.hasErrors()) {
            throw new BusinessException(valid.getFieldError().getDefaultMessage());
        }
        osmLogicNodeService.update(request);
    }

    /**
     * 删除
     *
     * @param id 节点Id
     */
    @ValidLogon
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @DeleteMapping("/{id}")
    public void delete(@ApiParam(required = true, name = "id", value = "节点Id") @PathVariable String id) {
        LOG.info("【逻辑节点】（删除）请求参数 -> {}", id);
        osmLogicNodeService.delete(id);
    }

    /**
     * 查询逻辑节点树
     *
     * @param node_name 节点名称
     */
    @ValidLogon
    @ApiOperation(value = "查询逻辑节点树", response = LogicNodeTree[].class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/tree")
    public Object tree(@ApiParam(name = "node_name", value = "节点名称") @RequestParam(required = false) String node_name) {
        LOG.info("【逻辑节点】（查询逻辑节点树）请求参数 -> {}", node_name);
        LogicNodeTree root = new LogicNodeTree();
        osmLogicNodeService.tree(root, null, node_name);
        return root.getChildren();
    }

    /**
     * 查询逻辑节点树
     *
     * @param id 节点Id
     */
    @ValidLogon
    @ApiOperation(value = "查询详情", response = LogicNodeTree[].class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/{id}/detail")
    public Object detail(@ApiParam(required = true, name = "id", value = "节点Id") @PathVariable String id) {
        return osmLogicNodeService.detail(id);
    }
}
